﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;

namespace Booking_Hotel
{
    public partial class Supplier_ViTri : System.Web.UI.Page
    {
        ThanhBHotelDataContext tbk = new ThanhBHotelDataContext();
        Supplier_Hotel_Vitri entities = new Supplier_Hotel_Vitri();
        int ViTriID;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["ss_SupplierID"] = 2;//Gan SupplierID cho Dropdownlist de lay DS khach san cua Supplier
                ShowViTri();
            }
        }

        protected void ShowViTri()
        {
            var vitri = from dv in tbk.Supplier_Hotels
                            join shv in tbk.Supplier_Hotel_Vitris on dv.Supplier_HotelID equals shv.Supplier_HotelID
                            where dv.SupplierID == (int)Session["ss_SupplierID"]
                            select new
                            {
                                Supplier_Hotel_ViTriID = shv.Supplier_Hotel_VitriID,
                                Supplier_HotelID = shv.Supplier_HotelID,
                                TenKhachSan = dv.TenKhachSan,
                                TenViTri = shv.TenViTri,
                                Description = shv.Description
                            };
            GridView1.DataSource = vitri;
            GridView1.DataBind();

        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow select = GridView1.Rows[e.NewSelectedIndex];
            Session["ss_Supplier_Hotel_ViTriID"] = int.Parse(select.Cells[0].Text.Trim());
            DDL_TenKhachSan.SelectedValue = select.Cells[1].Text.Trim();
            txt_TenViTri.Text = select.Cells[3].Text.Trim();
            txt_Desciption.Text = select.Cells[4].Text.Trim();
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                e.Row.Cells[0].Visible = false;
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton bt_Delete = sender as ImageButton;
            GridViewRow row = (GridViewRow)bt_Delete.NamingContainer;
            ViTriID = int.Parse(row.Cells[0].Text.Trim());//Supplier_Hotel_DichVuID trên lưới.
            Supplier_Hotel_Vitri shv = tbk.Supplier_Hotel_Vitris.Where(c => c.Supplier_Hotel_VitriID == ViTriID).Single();
            tbk.Supplier_Hotel_Vitris.DeleteOnSubmit(shv);
            tbk.SubmitChanges();
            ShowViTri();
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {

            try
            {
                entities = new Supplier_Hotel_Vitri();
                entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                entities.TenViTri = txt_TenViTri.Text.Trim();
                entities.Description = txt_Desciption.Text.Trim();
                Supplier_ViTriDAO sv = new Supplier_ViTriDAO();
                sv.addSupplier_Hotel_ViTri(entities);
                ShowViTri();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                int check_Supplier_Hotel_ViTriID = (int)(Session["ss_Supplier_Hotel_ViTriID"]);
                if (check_Supplier_Hotel_ViTriID != null)
                {
                    entities = new Supplier_Hotel_Vitri();
                    entities.Supplier_Hotel_VitriID = check_Supplier_Hotel_ViTriID;
                    entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                    entities.TenViTri = txt_TenViTri.Text.Trim();
                    entities.Description = txt_Desciption.Text.Trim();
                    Supplier_ViTriDAO sv = new Supplier_ViTriDAO();
                    sv.updateSupplier_Hotel_ViTri(entities);
                    ShowViTri();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Vui lòng chọn đối tượng cần update dưới lưới", this.Page);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}